import { observable, action } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
import * as Apis from '../apis/customercard';
import { Modal, message } from 'antd';
import { crmAddFormRules } from '../util/index';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class OutResourceStore {
    @observable tableStore = new TableStore();
    @observable rightMenu = [];
    @observable showAddForm = false;
    @observable form = new WeaForm();
    @observable addFormCondition =[];

    @observable loading = false;
    @observable baseParams = { operate: 'view' };
    @observable viewForm = new WeaForm();
    @observable viewFormCondition = [];


    @observable verified = false;
    @observable hasRight = false; // 权限
    @observable canEdit = false;

     // 刷新列表
     reLoad = () => {
       this.tableStore.getDatas(this.tableStore.dataKey, this.tableStore.current, this.tableStore.pageSize, this.tableStore.sortParams);
     }

    @action
    getOutResourceList=(params = {}) => {
      Apis.getOutResourceInfo(params).then((data) => {
        if (!data.hasright && typeof (data.hasright) !== 'undefined') {
          this.hasRight = false;
          this.verified = true;
        } else {
          this.hasRight = true;
          this.verified = true;
          this.tableStore.getDatas(data.sessionkey, params.current || 1);
          this.dataKey = data.sessionkey;
        }
      });
    }

    getOutReourceRightMenu=(params = {}) => {
      Apis.getRightMenuList(params).then((data) => {
        if (data.status == 'success') {
          this.rightMenu = data.datas;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 删除
    deleteOutResource= (params = {}, crmid) => {
      const that = this;
      Modal.confirm({
        title: getLabel(15172, '系统提示'),
        content: getLabel(83600, '您确认要删除吗？'),
        okText: getLabel(33703, '确定'),
        cancelText: getLabel(201, '取消'),
        onOk() {
          Apis.saveOutResourceAdd(params).then((data) => {
            if (data.status == 'success') {
              that.getOutResourceList({ customerId: crmid });
            } else {
              message.error(getLabel(83437, '出现错误！') + data.msgcode);
            }
          });
        },
        onCancel() {},
      });
    }
    // 新建
    outResourceAdd=(params = {}) => {
      this.showAddForm = true;
      this.form = new WeaForm();
      Apis.getOutResourceAdd(params).then((data) => {
        if (data.status == 'success') {
          this.addFormCondition = data.datas;
          this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    goback=() => {
      this.showAddForm = false;
    }
    // 新建保存
    saveOutResourceForm = (params) => {
      const formParams = this.form.getFormParams();
      const newParams = { ...formParams, ...params };
      this.form.validateForm().then((f) => {
        if (f.isValid) {
          if (newParams.password1 == newParams.password2) {
            if (newParams.seclevel && Number(newParams.seclevel) <= 0) {
              Apis.saveOutResourceAdd(newParams).then((data) => {
                if (data.status == 'success') {
                  this.showAddForm = false;
                  this.getOutResourceList({ customerId: newParams.customerId });
                } else {
                  message.error(getLabel(83437, '出现错误！') + data.msgcode);
                }
              });
            } else {
              Modal.warning({
                title: getLabel(15172, '系统提示'),
                content: getLabel(128198, '外部用户安全级别只能为负数'),
                okText: getLabel(33703, '确定'),
              });
            }
          } else {
            Modal.warning({
              title: getLabel(15172, '系统提示'),
              content: getLabel(125288, '两次密码输入不一致'),
              okText: getLabel(33703, '确定'),
            });
          }
        } else {
          f.showErrors();
          // Modal.warning({
          //     title: getLabel(15172,"系统提示"),
          //     content: getLabel(15859,"必要信息不完整，红色标记为必填项！"),
          //     okText:getLabel(33703,"确定")
          // });
        }
      });
    }
    //  编辑
    getOutResourceDetail =(params = {}) => {
      this.viewForm = new WeaForm();
      this.loading = true;
      this.baseParams.operate = 'edit';
      Apis.getOutResourceDetail(params).then((data) => {
        if (!data.hasright && typeof (data.hasright) !== 'undefined') {
          this.hasRight = false;
          this.verified = true;
        } else {
          this.hasRight = true;
          this.verified = true;
          this.loading = false;
          this.canEdit = data.canEdit;
          if (data.status == 'success') {
            this.baseParams = { ...this.baseParams, ...params };
            this.viewFormCondition = data.datas;
            this.viewForm && !this.viewForm.isFormInit && this.viewForm.initFormFields(data.datas);
          } else {
            message.error(getLabel(83437, '出现错误！') + data.msgcode);
          }
        }
      });
    }
     // 编辑保存
     saveOutResourceEdit = (params = {}) => {
       const formParams = this.viewForm.getFormParams();
       const newParams = { ...formParams, ...params };
       this.viewForm.validateForm().then((f) => {
         if (f.isValid) {
           if (newParams.password1 == newParams.password2) {
             if (newParams.seclevel && Number(newParams.seclevel) <= 0) {
               Apis.saveOutResourceAdd(newParams).then((data) => {
                 if (data.status == 'success') {
                   this.getOutResourceDetail({ id: newParams.id, operate: 'view' }, false);
                   window.opener._crmTable.reLoad();
                 } else {
                   message.error(getLabel(83437, '出现错误！') + data.msgcode);
                 }
               });
             } else {
               Modal.warning({
                 title: getLabel(15172, '系统提示'),
                 content: getLabel(128198, '外部用户安全级别只能为负数'),
                 okText: getLabel(33703, '确定'),
               });
             }
           } else {
             Modal.warning({
               title: getLabel(15172, '系统提示'),
               content: getLabel(125288, '两次密码输入不一致'),
               okText: getLabel(33703, '确定'),
             });
           }
         } else {
           f.showErrors();
           // Modal.warning({
           //     title: getLabel(15172,"系统提示"),
           //     content: getLabel(15859,"必要信息不完整，红色标记为必填项！"),
           //     okText:getLabel(33703,"确定")
           // });
         }
       });
     }


    clearStatus = () => {
      this.showAddForm = false;
    }
}
